package com.lovo.sys.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.sys.bean.*;
import com.lovo.sys.dao.IESDao;
import com.lovo.sys.service.IProductListingReviewService;
import com.lovo.sys.service.IProductService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@RestController
@RequestMapping("PLR")
public class ProductListingReviewController {
    @Autowired
    private IProductListingReviewService productListingReviewService;

    @Autowired
    private IProductService productService;
    @Autowired
    private IESDao esDao;
    @Autowired
    private RabbitTemplate template;

    //审核通过
    @GetMapping("updateOk/{productName}")
    public String updateOK(HttpServletRequest request, @PathVariable("productName") String productName) {
        //通过不发审核对象
        productService.updateStatus(productName);
        return "ok";
    }


    //审核不通过
    @GetMapping("updateNo/{msg}/{productName}")
    public String updateNo(HttpServletRequest request, @PathVariable("msg") String msg, @PathVariable("productName") String productName) {
        //获取登录对象
       AdminBean logUser = (AdminBean) request.getSession().getAttribute("loginUser");
        System.out.println(logUser);
        //封装数据
        ProductListingReviewBean reviewBean = new ProductListingReviewBean();
        reviewBean.setAdminName(logUser.getUserName());
//测试
//        reviewBean.setAdminName("tom");
        reviewBean.setProductStatus(2);
        reviewBean.setOperateDate(LocalDate.now());
        reviewBean.setProductName(productName);
        reviewBean.setContent(msg);
        productService.updateStatu(productName);

        //审核不通过发给前端
        String json = JSON.toJSONString(reviewBean);
        template.convertAndSend("SSc_Sys_Dev", "rabbit.audit", json);
        productListingReviewService.add(reviewBean);
        return "ok";
    }


    @GetMapping("findAll/{pageNO}")
    public IPage<ProductBean> findAll(@PathVariable("pageNO") int pageNO) {
        return productService.findAll(pageNO);
    }

    @GetMapping("findById/{id}")
    public EsBean findById(@PathVariable("id") String id) {
        EsBean byNewsId = esDao.findByNewsId(id);
        return byNewsId;
    }


}
